<!DOCTYPE html>
<html>
<head>
    <title data-culture="jobManagment">岗位管理</title>

</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12 col-sm-12 col-xs-12">
            <div class="widget  no-padding">
                <div class="widget-header bg-themeprimary">
                    <i class="widget-icon typcn typcn-th-list"></i>
                    <span class="widget-caption" data-culture="jobList">岗位列表</span>
                </div>
                <div class="widget-body">
                    <table id="grid-data">
                        <thead>
                        <tr>
                            <th style="text-align: center;" data-culture="jobName">岗位名称</th>
                            <th style="text-align: center;" data-culture="remark">备注</th>
                            <th style="text-align: center;" data-culture="sort">排序</th>
                            <th style="width: 200px; text-align: center;" data-culture="operation">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr class="row-no-display">
                            <td style="text-align: center;">$jobName$</td>
                            <td style="text-align: center;">$remark$</td>
                            <td style="text-align: center;">$sort$</td>
                            <td class="text-left" style="text-align: center;">
                                <a rel='editJob' class="btn btn-info btn-xs edit"><i
                                        class="fa fa-edit"></i><span data-culture="edit">编辑</span></a>
                                <!--<a rel='mappingUser' class="btn btn-yellow btn-xs edit"><i-->
                                        <!--class="fa fa-user"></i><span data-culture="mappingUser">分配用户</span></a>-->
                                <a rel='deleteJob' class="btn btn-danger btn-xs edit"><i
                                        class="fa fa-times"></i><span data-culture="delete">删除</span></a>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade form" id="editmodal" tabindex="-1" job="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="widget no-padding">
                <div class="widget-header bg-themeprimary">
                    <i class="widget-icon fa fa-user"></i>
                    <span class="widget-caption">岗位管理</span>
                    <div class="widget-buttons">
                        <a tabindex="7" data-type="reset"><i class="fa fa-refresh"></i></a>
                        <a data-dismiss="modal">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="widget-body">
                    <div>
                        <form class="form-horizontal" job="form" id="jobEditForm">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="jobName">岗位名称</label>
                                <div class="col-sm-10">
                                    <input class="form-control" id="jobName" name="jobName" type="text"
                                          placeholder="岗位名称">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="sort">序号</label>
                                <div class="col-sm-10">
                                    <input class="form-control" id="sort" name="sort" type="text"
                                           placeholder="序号">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="remark">备注</label>
                                <div class="col-sm-10">
                                    <input class="form-control" id="remark" name="remark" type="text"
                                           placeholder="备注">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-offset-8 col-sm-4" style="text-align: right">
                                    <a class="btn btn-success" href="#" id="btnSave"><i class="fa fa-check right"></i>保存</a>
                                    <a class="btn btn-danger" href="#" data-dismiss="modal"><i class="fa fa-times right"></i>关闭</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade form bs-example-modal-sm" tabindex="-1" job="dialog" aria-labelledby="mySmallModalLabel"
     aria-hidden="true" id="userTreeModal">
    <div class="modal-dialog modal-sm ">
        <div class="modal-content">
            <div class="widget no-padding">
                <div class="widget-header bg-themeprimary">
                    <i class="widget-icon glyphicon glyphicon-book"></i>
                    <span class="widget-caption">选择用户</span>
                    <div class="widget-buttons">
                        <a data-dismiss="modal">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="widget-body ctree">
                    <ul id="userTree" class="ztree" style="width:auto; overflow:auto;"></ul>
                </div>
                <div class="modal-footer">
                    <button type="button" id="btnMappingUser" class="btn btn-success">
                        确定
                    </button>
                </div>
            </div>
        </div>
    </div>

</div>
<script type="text/javascript">
    var jobObj = {
        insert: true,
        userTreeObj: null,
        currentJob: null,
        validateParam: {
            submitBtn:$("#btnSave"),
            submitHandler: function (obj) {
                obj.insert = jobObj.insert;
                if (!jobObj.insert) {
                    obj.id = jobObj.currentJob.id;
                }
                $.request('/admin/sys/job/save', obj, function (result) {
                    try {
                        $('#editmodal').modal('hide');
                        jobObj.grid.ext.refresh();
                    }
                    catch (ex) {
                    }
                }).always(function () {
                    $('#btnSave').button('reset');
                });
            },
            fields: {
                jobName: {
                    validators: {
                        notEmpty: {}
                    }
                },
                sort: {
                    validators: {
                        notEmpty: {},
                        dataType: {
                            dataType: 'decimal',
                            message: '只能输入数字'
                        }
                    }
                }

            }
        },
        grid: {
            keys: ['jobName'],
            url: "/admin/sys/job/query/page",
            placeholder: '岗位名称',
            stableparm: function () {
                return {};
            },
            events: {
                editJob: function (row) {
                    jobObj.insert = false;
                    jobObj.currentJob = row;
                    jobObj.validateParam.resetBtn.trigger("reset");
                    $.setobject(row, 'jobEditForm');
                    $('#editmodal').modal('show');
                },
                mappingUser: function (row) {
                    initUser(row.id);
                    $('#userTreeModal').modal('show');
                },
                deleteJob: function (row) {
                    bootbox.confirm('确认删除？', function () {
                        deleteRowList([id = row.id]);
                    });
                }

            },
            tools: [{
                text: '新增', 'data-culture': 'add', img: 'fa fa-plus', event: function (selectItems) {
                    jobObj.insert = true;
                    jobObj.validateParam.resetBtn.trigger("reset");
                    $('#jobName').val('');
                    $('#remark').val('');
                    $('#editmodal').modal('show');
                }
            }, {
                text: '编辑', 'data-culture': 'edit', img: 'fa fa-edit', event: function (selectItems) {
                    if (selectItems && selectItems.length == 1) {
                        jobObj.insert = false;
                        jobObj.validateParam.resetBtn.trigger("reset");
                        updateRow(selectItems[0]);
                    }
                    else {
                        bootbox.alert('请选择一条记录！');
                    }
                }
            }, {
                text: '删除', 'data-culture': 'delete', img: 'fa fa-times', event: function (selectItems) {
                    if (selectItems && selectItems.length > 0) {
                        bootbox.confirm('确认删除？', function () {
                            var ids = [];
                            $.each(selectItems, function (i, row) {
                                ids.push(row.id);
                            });
                            deleteRowList(ids);
                        });
                    } else {
                        bootbox.alert('请选择待删除的记录！');
                    }
                }
            }
//                , {
//                text: '分配用户', 'data-culture': 'mappingUser', img: 'fa fa-user', event: function (selectItems) {
//                    if (selectItems && selectItems.length > 0) {
//                        initUser(selectItems[0].id);
//                        $('#userTreeModal').modal('show');
//                    }
//                    else {
//                        bootbox.alert('请选择一条记录！');
//                    }
//                }
//            }
            ]
        }
    };

    function updateRow(job) {
        jobObj.currentJob = job;
        $.setobject(job, 'jobEditForm');
        $('#editmodal').modal('show');
    }
    function deleteRowList(ids) {
        $.request('/admin/sys/job/delete/ids', {ids: ids}, function () {
            bootbox.alert("删除成功!");
            jobObj.grid.ext.refresh();
        }, function () {
            bootbox.alert("删除失败!");
        })
    }
    function initUser(jobId) {
        var checkeds = [];
        $.request('/admin/sys/user/job/query/jobId', {jobId: jobId}, function (result) {
            $.each(result, function (i, v) {
                checkeds.push(v.userId);
            });
        }, false, false)
        jobObj.userTreeObj = $('#userTree').tree({
            data: {
                key: {name: 'orgName'},
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parentId",
                    rootPId: 0,
                    url: '/admin/sys/org/user/query/all'
                }
            },
            check: {
                enable: true
            },
            checkdata: checkeds,
            expandLevel: 3
        });

        $('#btnMappingUser').unbind('click').click(function () {
            var nodes = jobObj.userTreeObj.getCheckedNodes(true);
            var values = [];
            for (var i = 0; i < nodes.length; i++) {
                values.push({jobId: jobId, userId: nodes[i].id})
            }
            var url = '/admin/sys/user/job/save';
            if (values.length == 0) {
                values.push({jobId: jobId})
                url = '/admin/sys/user/job/delete/jobId';
            }
            $('#userTreeModal').modal('hide');
            $.request(url, values, function (result) {
                bootbox.alert('保存成功！');
            }, function (error) {
                bootbox.alert('保存失败！');
            }, true);
        })
    }
    function init() {
        $("#grid-data").table(jobObj.grid);
        $('#jobEditForm').validate(jobObj.validateParam);
    }
    function dispose() {
        jobObj = {};
    }
</script>
</body>
</html>